<?php

namespace App\Admin\Controllers;

use App\Models\Show;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class ShowController extends Controller
{
    use HasResourceActions;

    //活动图片首页
    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header('活动图片列表');
            $content->body($this->grid());
        });
    }

    //编辑活动图片
    public function edit($id, Content $content)
    {
        return $content->header('活动图片')->description('编辑')->body($this->form()->edit($id));
    }

    //编辑活动图片
    public function update($id)
    {
        return $this->form()->update($id);
    }

    //获取活动图片
    protected function grid()
    {
        return Admin::grid(Show::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->type('活动图片类别')->display(function ($value) {
                 switch ($value) {
                     case '1':
                         return '免费贴膜';
                     case '2':
                         return '加盟合作';
                 }
            });
            $grid->picture('活动图片')->display(function ($value) {
                return "<img style='width: 50%;height: 50%;' src='".$value."' />";
            });
            $grid->create_at('创建时间');

            $grid->disableCreateButton();

            $grid->disableRowSelector();

            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableView();
            });

            // 数据查询过滤
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();

            });

            $grid->model()->orderBy('id', 'desc');

            //禁用导出按钮
            $grid->disableExport();
        });
    }

    //修改表单事项
    protected function form()
    {
        // 创建一个表单
        return Admin::form(Show::class, function (Form $form) {
            $form->image('picture', '活动图片')->rules('image');

            // 定义事件回调，当模型即将保存时会触发这个回调
            $form->saving(function (Form $form) {
            });
            $form->tools(function (Form\Tools $tools) {
                // 去掉`删除`按钮
                $tools->disableDelete();
            });
        });
    }
}
